package sis.studentinfotest;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-27 11:32
 * @notify
 * @version 1.0
 */

import com.jimbob.ach.*;
import junit.framework.Assert;
import junit.framework.TestCase;
import sis.studentinfo.Account;
import sis.studentinfo.MockAch;

import java.math.BigDecimal;
import java.util.Date;

public class AccountTest extends TestCase {

    //企业号
    public static final String ABA = "102000012";
    //账号
    public static final String ACCOUNT_NUMBER = "194431518811";

    private Account account;

    protected void setUp() {
        account = new Account();
        //设置账号信息
        account.setBankAba(ABA);
        account.setBankAccountNumber(ACCOUNT_NUMBER);
        //设置交易类型
        account.setBankAccountType(Account.BankAccountType.CHECKING);
    }

    public void testWithdraw() throws Exception {
        account.credit(new BigDecimal("100.00"));
        account.withdraw(new BigDecimal("40"));
        assertEquals(new BigDecimal("60.00"), account.getBalance());
    }

    public void testWithdrawInsufficientFunds() {
        account.credit(new BigDecimal("100.00"));
        account.withdraw(new BigDecimal("140.00"));
        assertEquals(new BigDecimal("100.00"), account.getBalance());
    }

    private Ach createMockAch(AchStatus status) {
        return new MockAch() {
            public AchResponse issueDebit(AchCredentials credetials, AchTransactionData data) {
                //断言企业号 和账号 是否可用
                Assert.assertTrue(data.account.equals(AccountTest.ACCOUNT_NUMBER));
                Assert.assertTrue(data.aba.equals(AccountTest.ABA));

                //返回值
                AchResponse response = new AchResponse();
                //操作时间
                response.timestamp = new Date();
                //返回状态码
                response.traceCode = "1";
                //返回状态
                response.status = status;
                return response;
            }
        };
    }

    public void testTransferFromBank() {
//        //创建一个 银行对象
        account.setAch(createMockAch(AchStatus.SUCCESS));
        final BigDecimal amount = new BigDecimal("50.00");

        account.transferFromBank(amount);

        assertEquals(amount, account.getBalance());
    }

    public void testTransactions() {
        Account account = new Account();
        account.credit(new BigDecimal("1"));
        account.credit(new BigDecimal("11"));
        assertEquals(new BigDecimal("12.00"), account.getBalance());
    }

    public void testDecimal() {
        //加减乘除都出现了对应的精度问题
//        System.out.println(0.05 + 0.01); //0.060000000000000005
//        System.out.println(1.0 - 0.42); //0.5800000000000001
//        System.out.println(4.015 * 100); //401.49999999999994
//        System.out.println(123.3 / 100); //1.2329999999999999

        BigDecimal bigDecimal = new BigDecimal("0.05");

        BigDecimal bigDecima2 = new BigDecimal("0.01");

        BigDecimal bigDecima3 = bigDecimal.add(bigDecima2);

        assertEquals(0.06, bigDecima3.doubleValue());
    }

    public void testTransactionAverage() {
        Account account = new Account();
        account.credit(new BigDecimal("0.10"));
        account.credit(new BigDecimal("11.00"));
        account.credit(new BigDecimal("2.99"));
        assertEquals(new BigDecimal(4.70).doubleValue(), account.transactionAverage().doubleValue());
    }
}
